﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Browser;
using Microsoft.Practices.Prism.Commands;
using VS.MainModule.Models;
using Microsoft.Practices.Prism.Regions;

namespace VS.MainModule.Views
{
    public class ArticleViewModel : INotifyPropertyChanged, INavigationAware
    {

        #region Properties

        public ObservableCollection<XamlContent> Articles { get; private set; }
        public DelegateCommand<string> DownloadFileCommand { get; private set; }
        public DelegateCommand<string> MailToCommand { get; private set; }

        private int _selectedPage;
        public int SelectedPageIndex
        {
            get { return _selectedPage; }
            set
            {
                _selectedPage = value;
                OnPropertyChanged("SelectedPageIndex");
            }
        }

        #endregion

        public ArticleViewModel()
        {
            Articles = new ObservableCollection<XamlContent>()
            {
                new XamlContent()
                {
                    Id = "about",
                    Uri = "VS.MainModule.Articles.About.xaml",
                    Label = "Qui sommes-nous ? Que faisons-nous ?", 
                    ViewModel = this
                },                
                new XamlContent()
                {
                    Id = "technologies",
                    Uri = "VS.MainModule.Articles.Technologies.xaml",
                    Label = "Technologies", 
                    ViewModel = this
                },
                new XamlContent()
                {
                    Id = "realisations",
                    Uri = "VS.MainModule.Articles.Realisations.xaml",
                    Label = "Réalisations", 
                    ViewModel = this
                }
                
            };

            SelectedPageIndex = 0;

            DownloadFileCommand = new DelegateCommand<string>(x =>
            {
                Uri uri = new Uri(HtmlPage.Document.DocumentUri, string.Format("DownloadFile.ashx?file=Resources/{0}", x));
                HtmlPage.Window.Navigate(uri, "_blank");
            });

            MailToCommand = new DelegateCommand<string>(x =>
            {
                HtmlPage.Window.Navigate(new Uri(string.Format("mailto:{0}", x)), "_blank");
            });
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion


        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            XamlContent article = Articles.FirstOrDefault(x => x.Id == navigationContext.Parameters["p0"]);
            int pageIndex = article == null ? 0 : Articles.IndexOf(article);
            SelectedPageIndex = pageIndex;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
    }
}
